package com.jvtd.demo.zego;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jvtd.demo.App;
import com.jvtd.demo.R;
import com.jvtd.demo.app.MyApp;
import com.jvtd.zego.JvtdLiveView;
import com.jvtd.zego.JvtdZego;
import com.jvtd.zego.JvtdZegoUtils;
import com.zego.zegoliveroom.callback.IZegoLivePlayerCallback;
import com.zego.zegoliveroom.callback.IZegoLoginCompletionCallback;
import com.zego.zegoliveroom.callback.IZegoRoomCallback;
import com.zego.zegoliveroom.callback.im.IZegoIMCallback;
import com.zego.zegoliveroom.constants.ZegoConstants;
import com.zego.zegoliveroom.entity.ZegoBigRoomMessage;
import com.zego.zegoliveroom.entity.ZegoConversationMessage;
import com.zego.zegoliveroom.entity.ZegoRoomMessage;
import com.zego.zegoliveroom.entity.ZegoStreamInfo;
import com.zego.zegoliveroom.entity.ZegoStreamQuality;
import com.zego.zegoliveroom.entity.ZegoUserState;

public class ZegoActivity extends AppCompatActivity {
    private JvtdLiveView mJvtdLiveView;
    private EditText mEditText;
    private TextView mTextView;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zego);
        mJvtdLiveView = findViewById(R.id.live_view);
        JvtdZego.getInstance()
                .init(Long.valueOf(App.ZEGO_APP_ID), JvtdZegoUtils.parseSignKeyFromString(App.ZEGO_APP_KEY), MyApp.get(this))
                .isDevelopment(true)
                .initSdkContext()
                .initUserInfo(System.currentTimeMillis() + "", "Android_" + System.currentTimeMillis())
                .open();
        mTextView = findViewById(R.id.join_btn);
        mEditText = findViewById(R.id.edit_text);
//        mEditText.setText("38039351");
        mEditText.setText("#d-1540346066700");
        mTextView.setOnClickListener(v -> join());
        mJvtdLiveView.initLiveRoom(this, JvtdZego.getInstance().getLiveRoom());
    }

    private void join() {
        mJvtdLiveView.logout();
        mJvtdLiveView.loginRoom(mEditText.getText().toString(), (i, zegoStreamInfos) -> {
          if (i != 0)
            showMsg("加入失败：RoomID = " + mEditText.getText().toString() +
                    "\nerrorCode = " +  i);
          else
              showMsg("进入房间成功");
        }).setLivePlayerCallback(
                new IZegoLivePlayerCallback() {
                    @Override
                    public void onPlayStateUpdate(int i, String s) {
                    }

                    @Override
                    public void onPlayQualityUpdate(String s, ZegoStreamQuality zegoStreamQuality) {
                    }

                    @Override
                    public void onInviteJoinLiveRequest(int i, String s, String s1, String s2) {
                    }

                    @Override
                    public void onRecvEndJoinLiveCommand(String s, String s1, String s2) {
                    }

                    @Override
                    public void onVideoSizeChangedTo(String s, int i, int i1) {
                    }
                })
                .setRoomCallback(new IZegoRoomCallback() {
                    @Override
                    public void onKickOut(int i, String s) {

                    }

                    @Override
                    public void onDisconnect(int i, String s) {

                    }

                    @Override
                    public void onReconnect(int i, String s) {

                    }

                    @Override
                    public void onTempBroken(int i, String s) {

                    }

                    @Override
                    public void onStreamUpdated(int i, ZegoStreamInfo[] zegoStreamInfos, String s) {
                        showMsg("获取流信息");
                    }

                    @Override
                    public void onStreamExtraInfoUpdated(ZegoStreamInfo[] zegoStreamInfos, String s) {

                    }

                    @Override
                    public void onRecvCustomCommand(String s, String s1, String s2, String s3) {

                    }
                })
                .setIMCallback(new IZegoIMCallback() {
                    @Override
                    public void onUserUpdate(ZegoUserState[] zegoUserStates, int i) {

                    }

                    @Override
                    public void onRecvRoomMessage(String s, ZegoRoomMessage[] zegoRoomMessages) {

                    }

                    @Override
                    public void onRecvConversationMessage(String s, String s1, ZegoConversationMessage zegoConversationMessage) {

                    }

                    @Override
                    public void onUpdateOnlineCount(String s, int i) {

                    }

                    @Override
                    public void onRecvBigRoomMessage(String s, ZegoBigRoomMessage[] zegoBigRoomMessages) {

                    }
                });
    }

    private void showMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mJvtdLiveView.handleConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mJvtdLiveView.onDestroy();
    }
}

